<?php

class Asset {

	// Benstore Assets
	public static function cdn($asset) {

	    //Check if we added cdn's to the config file
	    if( !Config::get('app.cdn') )
	        return asset( $asset );

	    //Get file name & cdn's
	    $cdns = Config::get('app.cdn');
	    $assetName = basename( $asset );
	    //remove any query string for matching
	    $assetName = explode("?", $assetName);
	    $assetName = $assetName[0];

	    //Find the correct cdn to use
	    foreach( $cdns as $cdn => $types ) {
	        if( preg_match('/^.*\.(' . $types . ')$/i', $assetName) )
	            return self::cdnPath($cdn, $asset);
	    }

	    //If we couldnt match a cdn, use the last in the list.
	    end($cdns);
	    return self::cdnPath( key( $cdns ) , $asset);

	}

	public static function cdnPath($cdn, $asset) {
	    return  "//" . rtrim($cdn, "/") . "/" . ltrim( $asset, "/");
	}

	// Backend Assets
	public static function bea( $asset ) {

	    //Check if we added cdn's to the config file
	    if( !Config::get('app.bea') )
	        return asset( $asset );

	    //Get file name & cdn's
	    $cdns = Config::get('app.bea');
	    $assetName = basename( $asset );
	    //remove any query string for matching
	    $assetName = explode("?", $assetName);
	    $assetName = $assetName[0];

	    //Find the correct cdn to use
	    foreach( $cdns as $cdn => $types ) {
	        if( preg_match('/^.*\.(' . $types . ')$/i', $assetName) )
	            return self::beaPath($cdn, $asset);
	    }

	    //If we couldnt match a cdn, use the last in the list.
	    end($cdns);
	    return self::beaPath( key( $cdns ) , $asset);
	}

	public static function beaPath($cdn, $asset) {
	    return  "//" . rtrim($cdn, "/") . "/" . ltrim( $asset, "/");
	}
}